package tttrung43.yahoo.com.gdx;

import java.util.Iterator;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class TestGame implements ApplicationListener {

	OrthographicCamera camera;
	SpriteBatch batch;
	Texture bucket;
	Texture rain;
	Sound sound;
	Music music;
	float x, y;
	Rectangle recBucket;
	Rectangle recRain;
	float screenWidth = 800, screenHeight = 480;
	long lastDropTime;
	Array<Rectangle> recArray;
	int score=0;
	BitmapFont font;

	@Override
	public void create() {		
		camera = new OrthographicCamera();
		camera.setToOrtho(false, screenWidth, screenHeight);
		batch = new SpriteBatch();
		bucket = new Texture(Gdx.files.internal("data/bucket.png"));
		rain = new Texture(Gdx.files.internal("data/droplet.png"));
		sound = Gdx.audio.newSound(Gdx.files.internal("data/waterdrop.wav"));
		music = Gdx.audio.newMusic(Gdx.files.internal("data/rain.mp3"));
		music.setLooping(true);
		music.play();

		font = new BitmapFont();			
		font.setColor(Color.BLACK);	
		
		recBucket = new Rectangle();
		recBucket.width = 64;
		recBucket.height = 64;
		recBucket.x = (screenWidth - 64) / 2;
		recBucket.y = 20;
		recArray = new Array<Rectangle>();
		spawnDroplet();
	}

	@Override
	public void dispose() {

		batch.dispose();
	}

	@Override
	public void pause() {

	}

	void spawnDroplet() {
		recRain = new Rectangle();
		recRain.width = 64;
		recRain.height = 64;
		recRain.x = MathUtils.random(64, 736);
		recRain.y = 480;
		recArray.add(recRain);
		lastDropTime = TimeUtils.nanoTime();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		camera.update();
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		if (Gdx.input.isTouched()) {
			Vector3 touchPos = new Vector3();
			touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
			camera.unproject(touchPos);
			recBucket.x = touchPos.x - 32;
		}
		batch.draw(bucket, recBucket.x, recBucket.y);
		font.draw(batch, "Your score: "+score, 10, 480-20);
		
		for(Rectangle item : recArray){
			batch.draw(rain, item.x, item.y);
		}
		
		Iterator<Rectangle> iter = recArray.iterator();
		while (iter.hasNext()) {
			Rectangle item = iter.next();
			item.y -= 200 * Gdx.graphics.getDeltaTime();			
			if (item.y + 64 < 0)
				iter.remove();
			if (recBucket.overlaps(item)) {
				sound.play();
				iter.remove();
				score++;
			}
		}

		if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
			spawnDroplet();

		batch.end();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}
